import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.*;
import java.util.Vector;
import java.io.*;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;


public class GUI {

	private JFrame frmTracker;
	private JTextField textFieldPort;
	private static JTextPane textFieldConsole;
	private final Action action = new SwingAction();
	private static JTable table;
	public static Vector <Peer> peerList = new Vector<Peer>(100,10);

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUI window = new GUI();
					window.frmTracker.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public GUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmTracker = new JFrame();
		frmTracker.setTitle("Tracker 1.0");
		frmTracker.setResizable(false);
		frmTracker.setBounds(100, 100, 777, 577);
		frmTracker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmTracker.getContentPane().setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 83, 751, 455);
		frmTracker.getContentPane().add(tabbedPane);
		
		
		JPanel tabConsole = new JPanel();
		tabbedPane.addTab("Console", null, tabConsole, null);
		tabbedPane.setEnabledAt(0, true);
		tabConsole.setLayout(null);
		
		JPanel panelConsole = new JPanel();
		panelConsole.setBounds(10, 11, 726, 405);
		tabConsole.add(panelConsole);
		panelConsole.setLayout(null);
		
		JScrollPane scrollPaneConsole = new JScrollPane();
		scrollPaneConsole.setBounds(10, 11, 706, 383);
		panelConsole.add(scrollPaneConsole);
		
		textFieldConsole = new JTextPane();
		textFieldConsole.setBackground(Color.BLACK);
		textFieldConsole.setForeground(Color.GREEN);
		textFieldConsole.setEditable(false);
		textFieldConsole.setFont(new Font("Arial", Font.PLAIN, 11));
		scrollPaneConsole.setViewportView(textFieldConsole);
		
		JPanel tabPeers = new JPanel();
		tabbedPane.addTab("Peers", null, tabPeers, null);
		tabPeers.setLayout(null);
		
		JScrollPane scrollPaneTable = new JScrollPane();
		scrollPaneTable.setBounds(10, 11, 726, 405);
		tabPeers.add(scrollPaneTable);
		
		table = new JTable();

		TableModelPeers tableModelPeers = new TableModelPeers(GUI.peerList);
		table.setModel(tableModelPeers);
		
		scrollPaneTable.setViewportView(table);
		
		
		JLabel lblTracker = new JLabel("Tracker 1.0");
		lblTracker.setFont(new Font("Arial", Font.PLAIN, 11));
		lblTracker.setBounds(10, 28, 102, 14);
		frmTracker.getContentPane().add(lblTracker);
		
		JLabel lblPort = new JLabel("Port:   ");
		lblPort.setFont(new Font("Arial", Font.PLAIN, 11));
		lblPort.setBounds(122, 28, 46, 14);
		frmTracker.getContentPane().add(lblPort);
		
		textFieldPort = new JTextField();
		textFieldPort.setText("4444");
		textFieldPort.setBounds(151, 25, 86, 20);
		frmTracker.getContentPane().add(textFieldPort);
		textFieldPort.setColumns(10);
	
		JButton btnStartTracker = new JButton("Start Tracker");
		btnStartTracker.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(textFieldPort.getText().equalsIgnoreCase("")){
					textFieldConsole.setText(textFieldConsole.getText()+"\nYou must add a port to the tracker, some fields are required!");
				}
				else{
					try {
						int port = Integer.parseInt(textFieldPort.getText());
						updateConsole("Tracker started listening on port: "+port);
						TrackerThread temp = new TrackerThread(port);
						temp.start();
					  
					} 	catch (NumberFormatException e){
					   updateConsole("Port must be an integer");
					}   catch (BindException e){
						updateConsole("Tracker already started");
					}
				}
			}
		});
		btnStartTracker.setFont(new Font("Arial", Font.PLAIN, 11));
		btnStartTracker.setBounds(334, 24, 118, 23);
		frmTracker.getContentPane().add(btnStartTracker);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 761, 21);
		frmTracker.getContentPane().add(menuBar);
		
		JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);
		
		JMenu mnAbout = new JMenu("About");
		menuBar.add(mnAbout);
	}
	
	public static void updateConsole(String message){
			textFieldConsole.setText(textFieldConsole.getText()+"\n"+message);
	}
	
	public static void updateModel(){
		TableModelPeers temp = (TableModelPeers) table.getModel();
		temp.fireTableDataChanged();
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
		}
	}
}
